//
//  LZFilterViewPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/12/8.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZFilterViewPresenter.h"
#import "NetworkServiceManager.h"
#import "MJExtension.h"
#import "LZCuisineModel.h"
#import "LZHotAreaModel.h"
#import "LZPublicKeywordModel.h"

extern NSString *customer;

static NSString *kFilterKeyWordsURL = @"/clientsellerCookingStyle/list";     //获取筛选关键词,菜系和热门商区

@implementation LZFilterViewPresenter

#pragma mark -
#pragma mark 返回数据源
//section name 视频
+ (NSArray *)returnSectionNameForVideo
{
  return @[LZLocalizedString(@"distance", nil),LZLocalizedString(@"popular_areas", nil),LZLocalizedString(@"cuisine", nil),LZLocalizedString(@"cost_person", nil),LZLocalizedString(@"published_date", nil),LZLocalizedString(@"discounts", nil),LZLocalizedString(@"sort_by", nil)];
}

//section name  餐厅
+ (NSArray *)returnSectionNameForRestaurant
{
    return @[LZLocalizedString(@"distance", nil),LZLocalizedString(@"popular_areas", nil),LZLocalizedString(@"cuisine", nil),LZLocalizedString(@"cost_person", nil),LZLocalizedString(@"discounts", nil),LZLocalizedString(@"sort_by", nil),LZLocalizedString(@"others", nil)];
}

/**
 筛选 距离

 @return <#return value description#>
 */
+ (NSMutableArray *)returnDistanceArr
{
    NSArray *distanceArrTemp = @[@"500m",@"1000m",@"2000m",@"5000m"];
    NSMutableArray *distanceDataArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < distanceArrTemp.count; i ++) {
        LZPublicKeywordModel *m = [[LZPublicKeywordModel alloc] init];
        m.name = distanceArrTemp[i];
        [distanceDataArr addObject:m];
    }
    
    return distanceDataArr;

}



/**
 筛选  优惠

 @return <#return value description#>
 */
+ (NSMutableArray *)returnDiscountsArr
{
    //优惠
    NSMutableArray *discountsDataArr = [[NSMutableArray alloc] init];
    NSArray *arrTemp = @[LZLocalizedString(@"get_coupon", nil),LZLocalizedString(@"group_order", nil)];
    for (int i = 0 ; i < arrTemp.count; i ++) {
        LZPublicKeywordModel *m = [[LZPublicKeywordModel alloc] init];
        m.name = arrTemp[i];
        m.mNumber = i + 1;
        [discountsDataArr addObject:m];
    }
    
    return discountsDataArr;

}


/**
 视频筛选 排序

 @return <#return value description#>
 */
+ (NSMutableArray *)returnSortArrForVideo
{
    NSMutableArray *sortDataArr = [[NSMutableArray alloc] init];
    NSArray *arrTemp = @[LZLocalizedString(@"rating", nil),LZLocalizedString(@"height_price", nil),LZLocalizedString(@"low_price", nil)];
    for (int i = 0 ; i < arrTemp.count; i ++) {
        LZPublicKeywordModel *m = [[LZPublicKeywordModel alloc] init];
        m.name = arrTemp[i];
        
        [sortDataArr addObject:m];
    }
    
    return sortDataArr;

}


/**
 餐厅筛选  排序

 @return <#return value description#>
 */
+ (NSMutableArray *)returnSortArrForRestaurant
{
    NSMutableArray *sortDataArr = [[NSMutableArray alloc] init];
    NSArray *arrTemp = @[LZLocalizedString(@"rating", nil),LZLocalizedString(@"taste", nil),LZLocalizedString(@"atmosphere", nil),LZLocalizedString(@"service", nil),LZLocalizedString(@"height_price", nil),LZLocalizedString(@"low_price", nil)];
    for (int i = 0 ; i < arrTemp.count; i ++) {
        LZPublicKeywordModel *m = [[LZPublicKeywordModel alloc] init];
        m.name = arrTemp[i];
        
        [sortDataArr addObject:m];
    }
    
    return sortDataArr;
   
}


/**
 餐厅筛选 其他

 @return <#return value description#>
 */
+ (NSMutableArray *)returnOtherArr
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSArray *arrTemp = @[LZLocalizedString(@"is_open", nil)];
    for (int i = 0 ; i < arrTemp.count; i ++) {
        LZPublicKeywordModel *m = [[LZPublicKeywordModel alloc] init];
        m.name = arrTemp[i];
        
        [arr addObject:m];
    }
    
    return arr;
}


#pragma mark -
#pragma mark 操作,存储单选 多选
/**
 操作单选结果
 
 @param keyword 单选的关键字
 @param m 新传进来的model
 @param dataArr 原来的数据源
 */
+ (NSString *)operationRadioDataWithKeyword:(NSString *)keyword newSelectedModel:(LZPublicKeywordModel *)m dataSource:(NSMutableArray *)dataArr
{
    if (keyword.length > 0) {
        if (![keyword isEqualToString:m.name]) {
            for (int i = 0; i < dataArr.count; i ++) {
                LZPublicKeywordModel *model = dataArr[i];
                if ([model.name isEqualToString:keyword]) {
                    model.selected = NO;
                }
            }
            
            m.selected = YES;
            return m.name;
        }else{
            m.selected = NO;
            return nil;
        }
    }else{
        m.selected = YES;
        return m.name;
    }

}


/**
 操作 多选结果
 
 @param m 新传进来的model
 @param arr 原来已经存在的选择结果
 */
+ (void)operationMultiSelectDataWithModel:(id)m selectedArr:(NSMutableArray *)arr
{
    if ([m isKindOfClass:[LZPublicKeywordModel class]]) {
        LZPublicKeywordModel *mTemp = (LZPublicKeywordModel *)m;
        BOOL haveModel = [self checkTheSameModelName:mTemp.name InSelectedArr:arr];
        if (haveModel) {
            mTemp.selected = NO;
            [arr removeObject:mTemp];
        }else{
            mTemp.selected = YES;
            [arr addObject:mTemp];
        }
     
    }else if ([m isKindOfClass:[LZHotAreaModel class]]) {
        LZHotAreaModel *mTemp = (LZHotAreaModel *)m;
        BOOL haveModel = [self checkTheSameModelName:mTemp.name InSelectedArr:arr];
        if (haveModel) {
            mTemp.selected = NO;
            [arr removeObject:mTemp];
        }else{
            mTemp.selected = YES;
            [arr addObject:mTemp];
        }
       
    }else if ([m isKindOfClass:[LZCuisineModel class]]) {
        LZCuisineModel *mTemp = (LZCuisineModel *)m;
        BOOL haveModel = [self checkTheSameModelName:mTemp.name InSelectedArr:arr];
        if (haveModel) {
            mTemp.selected = NO;
            [arr removeObject:mTemp];
        }else{
            mTemp.selected = YES;
            [arr addObject:mTemp];
        }
       
    }

}

/**
 检查数组中是否包含该字符串
 
 @param name <#name description#>
 @param selectedArr <#selectedArr description#>
 @return <#return value description#>
 */
+ (BOOL)checkTheSameModelName:(NSString *)name InSelectedArr:(NSMutableArray *)selectedArr
{
    for (id subModel in selectedArr) {
        if ([subModel isKindOfClass:[LZPublicKeywordModel class]]) {
            LZPublicKeywordModel *m = (LZPublicKeywordModel *)subModel;
            if ([m.name isEqualToString:name]) {
                return YES;
            }
        }else if ([subModel isKindOfClass:[LZHotAreaModel class]]) {
            LZHotAreaModel *m = (LZHotAreaModel *)subModel;
            if ([m.name isEqualToString:name]) {
                return YES;
            }
        }else if ([subModel isKindOfClass:[LZCuisineModel class]]) {
            LZCuisineModel *m = (LZCuisineModel *)subModel;
            if ([m.name isEqualToString:name]) {
                return YES;
            }
        }
    }
    
    return NO;
}

#pragma mark -
#pragma mark 恢复上一次提交的值及状态
/**
 恢复距离选项 上一次的值
 
 @param keyword 上一次提交时存储的值
 @param dataArr 数据源
 */
+ (NSString *)restoreTheDistanceDataWithKeyword:(NSNumber *)keyword dataSource:(NSArray *)dataArr
{
    for (LZPublicKeywordModel *m in dataArr) {
        if ([m.name isEqualToString:[NSString stringWithFormat:@"%@m",keyword]]) {
            m.selected = YES;
        }else{
            m.selected = NO;
        }
    }
    
    return [NSString stringWithFormat:@"%@m",keyword];
}

/**
 恢复 排序选项 上一次的值
 
 @param dic 上一次提交时存储的字典值
 @param dataArr 数据源
 @return 返回上一次选择的值
 */
+ (NSString *)restoreTheSortDataWithKeywordDic:(NSMutableDictionary *)dic dataSource:(NSArray *)dataArr
{
    NSString *str = dic[@"field"];
    NSString *shengjiangxuStr = dic[@"order"];
    NSString *lastSelectedSt = @"";
    if ([str isEqualToString:@"averageScore" ] && [shengjiangxuStr isEqualToString:@"desc"]) {
        lastSelectedSt =  LZLocalizedString(@"rating", nil);
        
    }else if ([str isEqualToString:@"averageConsumption" ] && [shengjiangxuStr isEqualToString:@"desc"]) {
        lastSelectedSt =  LZLocalizedString(@"height_price", nil);
        
    }else if ([str isEqualToString:@"averageConsumption" ] && [shengjiangxuStr isEqualToString:@"asc"]) {
        lastSelectedSt =  LZLocalizedString(@"low_price", nil);
        
    }else if ([str isEqualToString:@"remarkCountTaste" ] && [shengjiangxuStr isEqualToString:@"desc"]) {
        lastSelectedSt =  LZLocalizedString(@"taste", nil);
        
    }else if ([str isEqualToString:@"remarkCountEnv" ] && [shengjiangxuStr isEqualToString:@"desc"]) {
        lastSelectedSt =  LZLocalizedString(@"atmosphere", nil);
        
    }else if ([str isEqualToString:@"remarkCountService" ] && [shengjiangxuStr isEqualToString:@"desc"]) {
        lastSelectedSt =  LZLocalizedString(@"service", nil);
    }
    
    for (LZPublicKeywordModel *m in dataArr) {
        if ([m.name isEqualToString:lastSelectedSt]) {
            m.selected = YES;
        }else{
            m.selected = NO;
        }
    }
    return lastSelectedSt;
}


/**
 恢复 其他选项 上一次的值
 
 @param dataArr 数据源
 @return 返回需要上一次的值
 */
+ (NSString *)restoreTheOtherDataWithdataSource:(NSArray *)dataArr
{
    for (LZPublicKeywordModel *m in dataArr) {
        m.selected = YES;
        
        return m.name;
    }
    
    return @"";
}




/**
 回复 热门商区 上一次选择的值

 @param hotAreaId 上一次提交时 存储的id串
 @param dataArr 数据源
 @return 返回已经选择的数据源
 */
+ (NSMutableArray *)restoreTheHotAreaDataWithHotAreaIDStr:(NSString *)hotAreaId DataSource:(NSArray *)dataArr
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSArray *idArr = [hotAreaId componentsSeparatedByString:@","];
    for (int  i = 0; i < dataArr.count ; i ++) {
        LZHotAreaModel *m = dataArr[i];
        if ([idArr containsObject:m.hotAreaID]) {
            m.selected = YES;
            [arr addObject:m];
        }else{
            m.selected = NO;
        }
    }
    
    return arr;
}


/**
 恢复 菜系 上一次选择的值
 
 @param cuisineId 上次提交时 存储的id 串
 @param dataArr 数据源
 @return 返回上次选择的数据源
 */
+ (NSMutableArray *)restoreTheCuisineDataWithCuisineIDStr:(NSString *)cuisineId DataSource:(NSArray *)dataArr
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSArray *idArr = [cuisineId componentsSeparatedByString:@","];
    for (int  i = 0; i < dataArr.count ; i ++) {
        LZCuisineModel *m = dataArr[i];
        if ([idArr containsObject:m.cuisineID]) {
            m.selected = YES;
            [arr addObject:m];
        }else{
            m.selected = NO;
        }
    }
    
    return arr;
}


/**
 恢复  优惠类型 上一次选择的值
 
 @param discountNumIdArr 上次提交时 存储的num 串
 @param dataArr 数据源
 @return 返回上次选择的数据源
 */
+ (NSMutableArray *)restoreTheDiscountsDataWithDiscountNumberIDArr:(NSArray *)discountNumIdArr DataSource:(NSArray *)dataArr
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < dataArr.count; i ++) {
        LZPublicKeywordModel *m = dataArr[i];
        if ([discountNumIdArr containsObject:[NSNumber numberWithInt:m.mNumber]]) {
            m.selected = YES;
            [arr addObject:m];
        }else{
            m.selected = NO;
        }
    }
    
    return arr;
}


#pragma mark -
#pragma mark 获取菜系, 热门商区
/**
 视频列表筛选项获取热门商区 菜系
 
 @param type 1 菜系      2商区
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getFilterKeyWordsWithType:(NSString *)type ResultSuccess:(void (^)(NSArray *))successBlock ResultFailed:(void (^)(NSString *))failedBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kFilterKeyWordsURL];
    NSDictionary *dic = @{@"type" : type};
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            NSMutableArray *listArrTemp = [[NSMutableArray alloc] init];
            NSArray *arrTemp = dic[@"data"];
            for (int i = 0; i < arrTemp.count; i ++) {
                NSDictionary *dic = arrTemp[i];
                if ([type isEqualToString:@"1"]) {
                    LZCuisineModel *m = [LZCuisineModel mj_objectWithKeyValues:dic];
                    [listArrTemp addObject:m];
                }else{
                    LZHotAreaModel *m = [LZHotAreaModel mj_objectWithKeyValues:dic];
                    [listArrTemp addObject:m];
                }
                
            }
            if (successBlock) {
                successBlock(listArrTemp);
            }
            
            
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}
@end
